package com.nova.edc.service.energy.impl;

import com.nova.edc.dao.energy.OverallStatisticsDao;
import com.nova.edc.model.energy.Energy;
import com.nova.edc.model.energy.OverallStatistics;
import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.model.system.ElecEnergy;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.service.energy.OverallStatisticsService;
import com.nova.edc.service.system.CustomerInfoService;
import com.nova.edc.service.system.ElecEnergyService;
import com.nova.frame.dao.parsing.SQLParser;
import com.nova.frame.utils.DateUtils;
import com.nova.frame.utils.JdbcUtils;
import com.nova.module.sql.SqlBuffer;
import com.seio.edc.util.AssitDateUtil;
import com.seio.edc.util.DateTimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/5 0005.
 */
public class OverallStatisticsServiceImpl implements OverallStatisticsService {
    @Autowired
    private OverallStatisticsDao novaOverallStatisticsDao;
    @Autowired
    private CustomerInfoService novaCustomerInfoService;
    @Autowired
    private ElecEnergyService novaElecEnergyService;

    @Override
    public List<OverallStatistics> getStatistics(List<CustomerInfo> list, String date) {
        List<Integer> customerinfoId = novaCustomerInfoService.getCustomerinfoId(list);
        SqlBuffer sqlBuffer = new SqlBuffer(SQLParser.sql("OverallStatisticsSQL.getStatistics"), true);
        List<OverallStatistics> query = JdbcUtils.query(OverallStatistics.class, sqlBuffer.getSelectSql().replace("#", org.apache.commons.lang3.StringUtils.join(customerinfoId, ",")), date);
        return query;
    }

    @Override
    public OverallStatistics insert(OverallStatistics overallStatistics) {
        return novaOverallStatisticsDao.insert(overallStatistics);
    }

    @Override
    public void insert(List<OverallStatistics> list) {
        for (OverallStatistics statistics : list) {
            insert(statistics);
        }
    }

    @Override
    public Map<String, BigDecimal> getAllStatistics(List<Integer> companyCodes) {
        SqlBuffer sqlBuffer = new SqlBuffer(
                SQLParser.sql("OverallStatisticsSQL.getAllStatistics"), true);
        List<Map<String, Object>> result = JdbcUtils.queryMap(sqlBuffer
                .getSelectSql().replace("#",
                        StringUtils.join(companyCodes, ",")));
        BigDecimal savePower = (BigDecimal) result.get(0).get(
                ModelConstant.SAVE_POWER);
        savePower = savePower == null ? BigDecimal.ZERO : savePower;
        BigDecimal saveCost = (BigDecimal) result.get(0).get(
                ModelConstant.SAVE_COST);
        saveCost = saveCost == null ? BigDecimal.ZERO : saveCost;
        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
        ElecEnergy elecEnergy = novaElecEnergyService
                .findByPowerCode(ModelConstant.SOCIAL_CO2);
        map.put(ModelConstant.SAVE_POWER, savePower);
        map.put(ModelConstant.SAVE_COST, saveCost);
        BigDecimal co2 = savePower.multiply(
                new BigDecimal(elecEnergy.getPowerstandardvalue())).divide(
                new BigDecimal(ModelConstant.SOCAIL_DIVIDE), 2);
        BigDecimal car = co2.divide(new BigDecimal(ModelConstant.SOCAIL_CAR),
                0, BigDecimal.ROUND_HALF_DOWN);
        map.put(ModelConstant.INDEX_CO2, co2);
        map.put(ModelConstant.INDEX_CAR, car);

        return map;
    }

    @Override
    public Map<String, BigDecimal> getUnitStatistics(String startDate, String endDate) {
        SqlBuffer sqlBuffer = new SqlBuffer(
                SQLParser.sql("OverallStatisticsSQL.getUnitStatistics"), true);
        List<Map<String, Object>> result = JdbcUtils.queryMap(
                sqlBuffer.getSelectSql(), startDate, endDate);
        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
        if (result.size() > 0) {
            Map<String, Object> resultMap = result.get(0);
            map.put(ModelConstant.UNIT_SAVE_POWER,
                    (BigDecimal) resultMap.get(ModelConstant.UNIT_SAVE_POWER));
            map.put(ModelConstant.UNIT_SAVE_COST,
                    (BigDecimal) resultMap.get(ModelConstant.UNIT_SAVE_COST));
        }
        return map;
    }

    @Override
    public Energy getEnergyByCompany(int companyCode, String startDate,
                                     String endDate) {
        SqlBuffer sqlBuffer = new SqlBuffer(
                SQLParser.sql("OverallStatisticsSQL.getAllPowerByCompanyCode"), true);
        Energy energy = JdbcUtils.get(Energy.class, sqlBuffer.getSql(),
                companyCode, startDate, endDate);
        return energy;
    }

    @Override
    public Map<String, BigDecimal> getStatisticsByDate(Integer companycode,
                                                       String startDate, String endDate) {
        SqlBuffer sqlBuffer = new SqlBuffer(
                SQLParser.sql("OverallStatisticsSQL.getStatisticsByDate"), true);
        List<Map<String, Object>> result = JdbcUtils.queryMap(
                sqlBuffer.getSelectSql(), companycode, startDate, endDate);
        BigDecimal savePower = (BigDecimal) result.get(0).get(
                ModelConstant.SAVE_POWER);
        savePower = savePower == null ? BigDecimal.ZERO : savePower;
        BigDecimal saveCost = (BigDecimal) result.get(0).get(
                ModelConstant.SAVE_COST);
        saveCost = saveCost == null ? BigDecimal.ZERO : saveCost;
        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();

        map.put(ModelConstant.SAVE_POWER, savePower);
        map.put(ModelConstant.SAVE_COST, saveCost);
        return map;

    }

    @Override
    public List<Map<String, Object>> getByMapArea(String position, Integer timeType) {
        SqlBuffer sqlBuffer = new SqlBuffer(SQLParser.sql("OverallStatisticsSQL.getByMapArea"), true);
        List<Map<String, Object>> query = JdbcUtils.queryMap(sqlBuffer.getSelectSql()
                .replace("#", "'" + ModelConstant.DEVICE_LIGHTENERGY + "','" + ModelConstant.DEVICE_AIR + "'")
                .replace("&", "'" + position + "'")
                .replace("$", getSearchSql(timeType)));
        return query;
    }

    public String getSearchSql(Integer type) {
        String addsql = "";
        switch (type) {
            case 0:
                addsql = " AND date_format(SummaryDate,'%Y-%m')= '" + DateUtils.getDate2Str("YYYY-MM", AssitDateUtil.getAddMonth(new Date(), -2)) + "'";
                break;
            case 1:
                addsql = " AND date_format(SummaryDate,'%Y-%m')= '" + DateUtils.getDate2Str("YYYY-MM", AssitDateUtil.getAddMonth(new Date(), -1)) + "'";
                break;
            case 2:
                addsql = " AND date_format(SummaryDate,'%Y-%m')= '" + DateTimeUtil.getNowTime("YYYY-MM") + "'";
                break;
            case 3:
                addsql = " AND date_format(SummaryDate,'%Y')= '" + DateTimeUtil.getNowTime("YYYY") + "'";
                break;
            case 4:
                addsql = " AND date_format(SummaryDate,'%Y')= '" + DateUtils.getDate2Str("YYYY", AssitDateUtil.getAddMonth(new Date(), -12)) + "'";
                break;
            case 5:
                addsql = "";
                break;
            default:
                addsql = " AND date_format(SummaryDate,'%Y-%m')= '" + DateTimeUtil.getNowTime("YYYY-MM") + "'";
        }
        return addsql;
    }
}
